<template>
  <div class="app-container">
    <!-- 顶部标题 -->
    <van-nav-bar title="基于Vue的移动商城系统"
                 @click-left="goBack">
      <van-icon :name="iconName"
                slot="left"
                color="#fff" />
    </van-nav-bar>
    <!-- 中间主体 -->
    <transition>
      <router-view></router-view>
    </transition>
    <!-- 底部 -->
    <van-tabbar v-model="active">
      <van-tabbar-item icon="home-o"
                       to="/home">首页</van-tabbar-item>
      <van-tabbar-item icon="shopping-cart-o"
                       :info="this.$store.getters.getCount"
                       to="/shopcart">购物车</van-tabbar-item>
      <van-tabbar-item icon="user-o"
                       to="/my">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>

export default {
  provide() {
    return {
      reload: this.reload
    }
  },
  data() {
    return {
      active: 0,
      iconName: ''
    }
  },
  methods: {
    // 导航返回
    goBack() {
      this.$router.back()
    }
  },
  watch: {
    // 监听路由地址改变底部tabbar和顶部返回按钮
    $route: function (next, prev) {
      let obj = { home: 0, shopcart: 1, my: 2 }
      this.active = obj[next.path.split('/')[1]]
      this.iconName = next.path == '/home' ? '' : 'arrow-left'
    }
  }
}
</script>

<style lang="scss">
.v-enter {
  transform: translateX(100%);
  opacity: 0;
}
.v-leave-to {
  opacity: 0;
  transform: translateX(-100%);
  position: absolute;
}
.v-enter-active,
.v-leave-active {
  transition: all 0.5s;
}
.app-container {
  padding-top: 1.22rem;
  padding-bottom: 1.3rem;
  max-width: 375px;
  margin: 0 auto;
  .van-nav-bar {
    background-color: #26a2ff;
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
    .van-nav-bar__title {
      color: #fff;
    }
  }
  .van-tabbar {
    border-top: 1px solid #ccc;
    .van-tabbar-item__icon {
      font-size: 0.6rem;
    }
  }
}
</style>
